<template>
	
	<view>
		<view class="wrap">
			<view class="u-tabs-box">
				<u-tabs-swiper activeColor="#f29100" ref="tabs" :list="list" :current="current" @change="change" :is-scroll="false" swiperWidth="750"></u-tabs-swiper>
			</view>
			<swiper class="swiper-box" :current="swiperCurrent" @transition="transition" @animationfinish="animationfinish">
<!-- 所有订单 zero -->
				<swiper-item v-for="(outItem,outIndex) in orderList" class="swiper-item">
					<scroll-view scroll-y style="height: 100%;width: 100%;" @scrolltolower="reachBottom">
						<view class="page-box">
							<view v-if="outItem" class="order" v-for="(res, index) in outItem" :key="res.id"@tap='jump(`pages/order/orderDetail`,res.id)'>
								<view class="top">
									<view class="left">
										<u-icon name="home" :size="30" color="rgb(94,94,94)"></u-icon>
										<view class="store">{{ res.createdTime | formatDate }}</view>
										<u-icon name="arrow-right" color="rgb(203,203,203)" :size="26"></u-icon>
									</view>
									<view v-show="res.status == 0" class="right">交易关闭</view>
									<view v-show="res.status == 1" class="right">等待支付</view>
									<view v-show="res.status == 2" class="right">等待发货</view>
									<view v-show="res.status == 3" class="right">等待收货</view>
									<view v-show="res.status == 4" class="right">等待评论</view>
									<view v-show="res.status == 5" class="right">交易完成</view>
								</view>
								<view class="item" v-for="(item, inIndex) in res.details" :key="inIndex">
									<view class="left"><image :src="vuex_user.store + item.book.img" mode="aspectFill"></image></view>
									<view class="content">
										<view class="title u-line-2">{{ item.book.name }}</view>
										<view class="type">{{ item.book.author }}</view>
										<!-- <view class="delivery-time">发货时间 {{ item.deliveryTime }}</view> -->
									</view>
									<view class="right">
										<view class="price">
											￥{{ item.price | rounding }}
											
										</view>
										<view class="number">x{{ item.count }}</view>
										<view class="evaluate " v-show="res.status == 4">
											<u-button type="warning" size="mini" @click="toEvaluate(res.id,item.id)">去评论</u-button>
										</view>
									</view>
									
								</view>
								<view class="total">
									共{{ res.details.length }}件商品 合计:
									<text class="total-price">
										￥{{ res.price | rounding }}
										
									</text>
								</view>
								<view class="bottom">
									<view class="more"></view>
									<view class="evaluate " v-show="res.status == 0 || res.status == 4">
										<u-button type="error" size="mini" @click="delOrder(res.id)">删除订单</u-button>
									</view>
									<view class="evaluate " v-show="res.status == 1">
										<u-button type="warning" size="mini">去支付</u-button>
									</view>
									<view class="evaluate " v-show="res.status == 2 ">
										<u-button type="primary" size="mini" @click='jump(`pages/order/orderDetail`,res.id)'>查看详情</u-button>
									</view>
									<view class="evaluate " v-show="res.status==3">
										<u-button type="primary" size="mini" @click="confirmReceipt(res.id)">确认收货</u-button>
									</view>
									<view class="evaluate " v-show="res.status==5">
										<u-button type="primary" size="mini" @click="delOrder(res.id)">删除订单</u-button>
									</view>

								</view>
							</view>
							<!-- 如果没有数据就显示空 -->
							<view v-if="outItem.length==0">
								<view class="centre">
									<image src="https://cdn.uviewui.com/uview/template/taobao-order.png" mode=""></image>
									<view class="explain">
										您还没有相关的订单
										<view class="tips">可以去看看有那些想买的</view>
									</view>
									<view class="btn" @click="btnToIndex">随便逛逛</view>
								</view>
							</view>
							<u-loadmore v-show="orderList[outIndex].length>0" class="u-m-b-20" :status="loadStatus[outIndex]" bgColor="#f2f2f2"></u-loadmore>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
			<u-modal v-model="delModal" content="确定删除订单吗" title="删除" @confirm="delConfirm" :show-cancel-button="true"></u-modal>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			orderList: [[], [], [], [], []], //0 所有 1 待付款 2 代发货  3 待收货  4 待评价
			
			orderPageNum:[1, 1, 1, 1, 1],
			orderTotalPage:[],
			
			list: [
				{
					name: '全部'
				},
				{
					name: '待付款'
				},
				{
					name: '待发货'
				},
				{
					name: '待收货'
				},
				{
					name: '待评价',
				}
			],
			current: 0,
			swiperCurrent: 0,
			tabsHeight: 0,
			dx: 0,
			loadStatus: ['loading','loading','loading','loading','loading'],
			delModal:false,
			delId:0,
			
		};
	},
	onLoad() {
		this.getAllOrderList()

	},

	methods: {
		//删除订单弹窗确认按钮事件
		async delConfirm(){
			var delOrderId = this.delId
			const res = await this.$u.api.delOrder(this.delId)
			if(res==null){
				for(let i = 0; i < this.orderList.length ; i++){
					this.orderList[i] = this.orderList[i].filter(function (item, index, array) {
						if (item.id != this.delId) {
							return true;
						}
					});
				}
			}
			
		},
		//删除订单
		async delOrder(val){
			this.delModal = true
			this.delId = val

		},
		//去评价
		toEvaluate(val,val2){
			// console.log(val,val2)
			this.$u.route({
				url:'pages/order/orderEvaluate',
				params:{
					oid:val,
					did:val2
				}
			})
		},
		//订单确认收货
		async confirmReceipt(val){
			const res = await this.$u.api.confirmReceipt(val)
			if(res==null){
				this.$u.toast("确认收货成功！快去评价商品吧")
				this.swiperCurrent = 4
				this.$u.route({
					url:'pages/order/orderEvaluate',
					params:{
						oid:val
					}
				})
			}
		},
		
		// 订单详情跳转
		jump(url, id) {
			this.$u.route({
				url,
				params: {
					oid:id
				}
			})
		},
		//跳转首页
		btnToIndex(){
			this.$u.route({
				type:'tab',
				url:'pages/index/index'
			})
		},
		// 下滑加载
		async getOrderList(val){
			if(val==0){
				var params = {
					pageNum:++this.orderPageNum[0]
				}
			}else{
				var params = {
					pageNum:++this.orderPageNum[val],
					status:val
				}
			}
			const res = await this.$u.api.getOrderList(params)
			console.log(res)
			if(res.list.length == 0){
				this.loadStatus[val] = 'nomore'
				console.log(this.loadStatus[val])
			}else{
				this.orderList[val].push(...res.list)
			}
		},
		
		//获取全部订单并分类
		async getAllOrderList(){
			const res = await this.$u.api.getOrderList()
			
			this.orderList[0].push(...res.list)
			if(res.totalPage<=1){
				this.loadStatus[0]='nomore'
			}else{
				this.orderTotalPage[0] = res.totalPage
			}
			
			
			for (var i = 1; i <= 4; i++) {
				const result = await this.$u.api.getOrderList({
					status:i
				})
				this.orderList[i].push(...result.list) 
				if(result.totalPage<=1){
					this.loadStatus[i]='nomore'
				}else{
					this.orderTotalPage[i] = result.totalPage
				}
				
			}
		},
		
		reachBottom() {
			let index = this.swiperCurrent
			// 判断 如果页数小于总页数  就去获取数据
			if(this.orderPageNum[index]+1 <= this.orderTotalPage[index]){
				console.log(index)
				console.log('this.orderPageNum[index]' + this.orderPageNum[index])
				console.log('this.orderTotalPage[index]' + this.orderTotalPage[index])
				this.getOrderList(index)
				
			}else{
				//  否则将状态改成 nomore
				this.loadStatus[index] = 'nomore'
			}
			
			
			
			
			// // 此tab为空数据
			// if(this.current != 2) {
			// 	this.loadStatus.splice(this.current,1,"loading")
			// 	setTimeout(() => {
			// 		this.getOrderList(this.current);
			// 	}, 12000);
			// }
			
		},

		// tab栏切换
		change(index) {
			this.swiperCurrent = index;
			console.log(this.swiperCurrent)
			// this.getOrderList(index);
		},
		transition({ detail: { dx } }) {
			this.$refs.tabs.setDx(dx);
		},
		animationfinish({ detail: { current } }) {
			this.$refs.tabs.setFinishCurrent(current);
			this.swiperCurrent = current;
			this.current = current;
		}
	}
};
</script>

<style>
/* #ifndef H5 */
page {
	height: 100%;
	background-color: #f2f2f2;
}
/* #endif */
</style>

<style lang="scss" scoped>
.order {
	width: 710rpx;
	background-color: #ffffff;
	margin: 20rpx auto;
	border-radius: 20rpx;
	box-sizing: border-box;
	padding: 20rpx;
	font-size: 28rpx;
	.top {
		display: flex;
		justify-content: space-between;
		.left {
			display: flex;
			align-items: center;
			.store {
				margin: 0 10rpx;
				font-size: 32rpx;
				font-weight: bold;
			}
		}
		.right {
			color: $u-type-warning-dark;
		}
	}
	.item {
		display: flex;
		margin: 20rpx 0 0;
		.left {
			margin-right: 20rpx;
			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
			}
		}
		.content {
			.title {
				font-size: 28rpx;
				line-height: 50rpx;
			}
			.type {
				margin: 10rpx 0;
				font-size: 24rpx;
				color: $u-tips-color;
			}
			.delivery-time {
				color: #e5d001;
				font-size: 24rpx;
			}
		}
		.right {
			margin-left: 10rpx;
			padding-top: 20rpx;
			text-align: right;
			.decimal {
				font-size: 24rpx;
				margin-top: 4rpx;
			}
			.number {
				color: $u-tips-color;
				font-size: 24rpx;
			}
		}
	}
	.total {
		margin-top: 20rpx;
		text-align: right;
		font-size: 24rpx;
		.total-price {
			font-size: 32rpx;
		}
	}
	.bottom {
		display: flex;
		margin-top: 40rpx;
		padding: 0 10rpx;
		justify-content: space-between;
		align-items: center;
		.btn {
			line-height: 52rpx;
			width: 160rpx;
			border-radius: 26rpx;
			border: 2rpx solid $u-border-color;
			font-size: 26rpx;
			text-align: center;
			color: $u-type-info-dark;
		}
		.evaluate {
			color: $u-type-warning-dark;
			border-color: $u-type-warning-dark;
		}
	}
}
.centre {
	text-align: center;
	margin: 200rpx auto;
	font-size: 32rpx;
	image {
		width: 164rpx;
		height: 164rpx;
		border-radius: 50%;
		margin-bottom: 20rpx;
	}
	.tips {
		font-size: 24rpx;
		color: #999999;
		margin-top: 20rpx;
	}
	.btn {
		margin: 80rpx auto;
		width: 200rpx;
		border-radius: 32rpx;
		line-height: 64rpx;
		color: #ffffff;
		font-size: 26rpx;
		background: linear-gradient(270deg, rgba(249, 116, 90, 1) 0%, rgba(255, 158, 1, 1) 100%);
	}
}
.wrap {
	display: flex;
	flex-direction: column;
	height: calc(100vh - var(--window-top));
	width: 100%;
}
.swiper-box {
	flex: 1;
}
.swiper-item {
	height: 100%;
}
</style>
